﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace UI
{
    public partial class ResultPage : System.Web.UI.Page
    {
        private DataTable dtResult;
        private int resultCount;

        protected void Page_Load(object sender, EventArgs e)
        {
            dtResult = new DataTable();
            string keyword = ""; 
            int page = 1;
            try
            {
                keyword = Request.QueryString["Kw"].ToString();
                page = Convert.ToInt32(Request.QueryString["Pg"]);
            }
            catch
            {
                page = 1;
            }
            if (!IsPostBack)
            {
                txtKeyword.Text = keyword;
            }
            if (keyword.Trim() != "")
            {
                LoadResult(GetResultByKwPg(keyword, page));
                //lblPage.Text = page.ToString() + " / " + pageCount.ToString();
                SetPageButton(this.resultCount, page);
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = txtKeyword.Text;
            Response.Redirect("ResultPage.aspx?Kw=" + Server.UrlEncode(keyword) + "&Pg=1");
        }

        private DataTable GetResultByKwPg(string keyword, int page)
        {
            DAL.DataAccess dataAccess = new DAL.DataAccess();
            //pageCount = dataAccess.GetResultCount(keyword) / 20 + 1;
            DataTable dt = dataAccess.GetResultTable(keyword, page);
            resultCount = dt.Rows.Count;
            return dt;
        }

        private void LoadResult(DataTable dtResult)
        {
            foreach (DataRow dr in dtResult.Rows)
            {
                string title = dr["Title"].ToString();
                if (title.Length > 50)
                {
                    title = title.Substring(0, 50);
                    title += " ... ";
                }
                string url = dr["Url"].ToString();
                string urlLink = "<a href=\'" + url + "\' target='_blank'>" + title + "</a>";
                UI.UserCtrl.LinkPanel linkPanel = (UI.UserCtrl.LinkPanel)LoadControl("~/UserCtrl/LinkPanel.ascx");
                linkPanel.LinkTitle = urlLink;
                linkPanel.Link = url;
                this.dvSearchBody.Controls.Add(linkPanel);
            }
        }

        protected void btnNext_Click(object sender, ImageClickEventArgs e)
        {
            int page = 1;
            try
            {
                page = Convert.ToInt32(Request.QueryString["Pg"]);
            }
            catch { page = 1; }
            string keyword = Request.QueryString["Kw"].ToString();
            page++;
            Response.Redirect("ResultPage.aspx?Kw=" + Server.UrlEncode(keyword) + "&Pg=" + page.ToString());
        }

        protected void btnPrevious_Click(object sender, ImageClickEventArgs e)
        {
            int page = 1;
            try
            {
                page = Convert.ToInt32(Request.QueryString["Pg"]);
            }
            catch { page = 1; }
            
            string keyword = Request.QueryString["Kw"].ToString();
            page--;
            if (page <= 0)
            {
                page = 1;
            }
            Response.Redirect("ResultPage.aspx?Kw=" + Server.UrlEncode(keyword) + "&Pg=" + page.ToString());
        }

        private void SetPageButton(int resultCount, int page)
        {
            if (resultCount < 20)
            {
                btnNext.Visible = false;
            }
            else
            {
                btnNext.Visible = true;
            }

            if (page == 1)
            {
                btnPrevious.Visible = false;
            }
            else
            {
                btnPrevious.Visible = true;
            }
        }
    }
}
